<?php

namespace app\admin\controller;
use app\admin\model\UserIntegral as UserIntegralModel;
use think\Db;

class UserIntegral extends Base {

    protected $user_integral_model;

    public function _initialize() {
        parent::_initialize();
        $this->user_integral_model = new UserIntegralModel();
    }

    /**
     * 用户积分列表
     * @param string
     * @param int
     * @return string
     */
    public function index() {
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $data = input('get.');
            $list = $this->user_integral_model->getListInfo($page, $limit, $data);
            echo json_encode($list);
            exit;
        }
        return $this->fetch();
    }
    //用户积分明细
    public function look(){
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $data = input('get.');
            $list = $this->user_integral_model->getListDetail($page, $limit, $data);
            echo json_encode($list);
            exit;
        }
        $uid = input('get.uid');
        $this->assign('uid',$uid?$uid:0);
        return $this->fetch();
    }
    
    //积分规则设置
    public function add(){
        if(input('post.')){
            $data = input('post.');
            $data['user_ratio']             = 1/$data['user_ratio'];
            $data['first_referrer_ratio']   = 1/$data['first_referrer_ratio'];
            $data['second_referrer_redio']  = 1/$data['second_referrer_redio'];
            if(!empty($data['id'])){
                Db::name('integral_reward_setting')->where(['id'=>$data['id']])->update($data);
            }else{
                Db::name('integral_reward_setting')->insertGetId($data);
            }
            echo ajax_success('提交成功');die;
        }else{
           $result = Db::name('integral_reward_setting')->where(['status'=>1])->order('id desc')->find();
           if(!empty($result)){
               $result['user_ratio']             = 1/$result['user_ratio'];
               $result['first_referrer_ratio']   = 1/$result['first_referrer_ratio'];
               $result['second_referrer_redio']  = 1/$result['second_referrer_redio'];
           }
           $this->assign('result',$result);
        }
        return $this->fetch();
    }


}
